﻿package com.tomvian.twodee.common
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	import sfb.engines.core.control.Controller;
	import com.tomvian.twodee.common.actions.Action;
	import com.tomvian.twodee.common.interactions.Interaction;
	import com.tomvian.maths.Vector2D;
	import sfb.physics.Mass;
	
	/**
	 * Actor
	 * @author Thomas Vian
	 */
	public class Actor extends Sprite
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		
		public var defaultAction:Action;
		
		public var controller:Controller;
		public var mass:Mass;
		public var position:Vector2D;
		public var velocity:Vector2D;
		public var friction:Vector2D;
		
		public var drag:Number = 0.95;
		
		public var direction:uint;
		public var angle:Number;
		
		public var actionDisplay:StateDisplay;
		
		public var cameraOffset:Vector2D;
		
		protected var _currentAction:Action;
		
		//--------------------------------------------------------------------------
		//	
		// Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		public function get currentAction():Action { return _currentAction; }
		public function set currentAction(value:Action):void
		{
			if (value != _currentAction)
			{
				if (_currentAction) _currentAction.stop();
				_currentAction = value;
				_currentAction.start();
			}
		}
		
		//--------------------------------------------------------------------------
		//	
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function Actor()
		{
			controller = new Controller();
			mass = new Mass();
			position = mass.position;
			velocity = mass.velocity;
			friction = mass.friction;
			
			cameraOffset = new Vector2D();
			
			actionDisplay = new StateDisplay(new MovieClip);
		}
		
		//--------------------------------------------------------------------------
		//	
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function setPosition(x:Number, y:Number):void
		{
			this.x = x;
			this.y = y;
			
			position.x = x;
			position.y = y;
		}
		
		public function act():void
		{
			friction.multiplyMod(drag);
			
			if (!_currentAction) currentAction = defaultAction;
			
			_currentAction.checkBranchActions();
			
			_currentAction.act();
			
			_currentAction.display();
			
			mass.renderToDO(this);
		}
		
		public function returnToDefault():void
		{
			currentAction = defaultAction;
			currentAction.act();
			currentAction.display();
		}
	}
}